﻿<script type="text/javascript">
	var Flower = function(){};
	var xiaoming = {
		sendFlower: function( target ){
			var flower = new Flower();
			target.receiveFlower( flower );
		}
	};
	var A = {
		receiveFlower: function( flower ){
			console.log( '收到花 ' + flower );
		}
	};
	xiaoming.sendFlower( A );


	//接下来，我们引入代理B，即小明通过B 来给A 送花：
	var Flower = function(){};
	var xiaoming = {
		sendFlower: function( target){
			var flower = new Flower();
			target.receiveFlower( flower );
		}
	};
	var B = {
		receiveFlower: function( flower ){
			A.receiveFlower( flower );
			90 第6 章 代理模式
		}
	};
	var A = {
		receiveFlower: function( flower ){
			console.log( '收到花 ' + flower );
		}
	};
	xiaoming.sendFlower( B );

	//然后选择A 心情好的时候把花转交给A，代码如下：

	var Flower = function(){};
	var xiaoming = {
		sendFlower: function( target){
			var flower = new Flower();
			target.receiveFlower( flower );
		}
	};
	var B = {
		receiveFlower: function( flower ){
			A.listenGoodMood(function(){ // 监听A 的好心情
				A.receiveFlower( flower );
			});
		}
	};

	var A = {
		receiveFlower: function( flower ){
			console.log( '收到花 ' + flower );
		},
		listenGoodMood: function( fn ){
			setTimeout(function(){ // 假设10 秒之后A 的心情变好
				fn();
			}, 10000 );
		}
	};

	xiaoming.sendFlower( B );

	var myImage = (function(){
		var imgNode = document.createElement( 'img' );
		document.body.appendChild( imgNode );
		return {
			setSrc: function( src ){
				imgNode.src = src;
			}
		}
	})();
	myImage.setSrc( 'http:// imgcache.qq.com/music/photo/k/000GGDys0yA0Nk.jpg' );

	var myImage = (function(){
		var imgNode = document.createElement( 'img' );
		document.body.appendChild( imgNode );
		return {
			setSrc: function( src ){
				imgNode.src = src;
			}
		}
	})();
	var proxyImage = (function(){
		var img = new Image;
		img.onload = function(){
			myImage.setSrc( this.src );
		}
		return {
			setSrc: function( src ){
				myImage.setSrc( 'file:// /C:/Users/svenzeng/Desktop/loading.gif' );
				img.src = src;
			}
		}
	})();
	proxyImage.setSrc( 'http:// imgcache.qq.com/music/photo/k/000GGDys0yA0Nk.jpg' );

	var MyImage = (function(){
		var imgNode = document.createElement( 'img' );
		document.body.appendChild( imgNode );
		var img = new Image;
		img.onload = function(){
			imgNode.src = img.src;
		};
		return {
			setSrc: function( src ){
				imgNode.src = 'file:// /C:/Users/svenzeng/Desktop/loading.gif';
				img.src = src;
			}
		}
	})();
	MyImage.setSrc( 'http:// imgcache.qq.com/music/photo/k/000GGDys0yA0Nk.jpg' );

	var myImage = (function(){
		var imgNode = document.createElement( 'img' );
		document.body.appendChild( imgNode );
		return function( src ){
			imgNode.src = src;
		}
	})();
	var proxyImage = (function(){
		var img = new Image;
		img.onload = function(){
			myImage( this.src );
		}
		return function( src ){
			myImage( 'file:// /C:/Users/svenzeng/Desktop/loading.gif' );
			img.src = src;

		}
	})();
	proxyImage( 'http:// imgcache.qq.com/music// N/k/000GGDys0yA0Nk.jpg' );

</script>

<body>
	<input type="checkbox" id="1"></input>1
	<input type="checkbox" id="2"></input>2
	<input type="checkbox" id="3"></input>3
	<input type="checkbox" id="4"></input>4
	<input type="checkbox" id="5"></input>5
	<input type="checkbox" id="6"></input>6
	<input type="checkbox" id="7"></input>7
	<input type="checkbox" id="8"></input>8
	<input type="checkbox" id="9"></input>9
</body>

<script type="text/javascript">
	//接下来，给这些checkbox 绑定点击事件，并且在点击的同时往另一台服务器同步文件：
	var synchronousFile = function( id ){
		console.log( '开始同步文件，id 为: ' + id );
	};
	var checkbox = document.getElementsByTagName( 'input' );
	for ( var i = 0, c; c = checkbox[ i++ ]; ){
		c.onclick = function(){
			if ( this.checked === true ){
				synchronousFile( this.id );
			}
		}
	};

	var synchronousFile = function( id ){
		console.log( '开始同步文件，id 为: ' + id );
	};

	var proxySynchronousFile = (function(){
		var cache = [], // 保存一段时间内需要同步的ID
		timer; // 定时器
		return function( id ){
			cache.push( id );
			if ( timer ){ // 保证不会覆盖已经启动的定时器
				return;
			}
			timer = setTimeout(function(){
			synchronousFile( cache.join( ',' ) ); // 2 秒后向本体发送需要同步的ID 集合
			clearTimeout( timer ); // 清空定时器
			timer = null;
			cache.length = 0; // 清空ID 集合
		}, 2000 );
		}
	})();

	var checkbox = document.getElementsByTagName( 'input' );
	for ( var i = 0, c; c = checkbox[ i++ ]; ){
		c.onclick = function(){
			if ( this.checked === true ){
				proxySynchronousFile( this.id );
			}
		}
	};

</script>


<script type="text/javascript">
	var cache = [];
	var miniConsole = {
		log: function(){
			var args = arguments;
			cache.push( function(){
				return miniConsole.log.apply( miniConsole, args );
			});
		}
	};
	miniConsole.log(1);

	var handler = function( ev ){
		if ( ev.keyCode === 113 ){
			var script = document.createElement( 'script' );
			script.onload = function(){
				for ( var i = 0, fn; fn = cache[ i++ ]; ){
					fn();
				}
			};
			script.src = 'miniConsole.js';
			document.getElementsByTagName( 'head' )[0].appendChild( script );
		}
	};
	document.body.addEventListener( 'keydown', handler, false );

	// miniConsole.js 代码：
	miniConsole = {
		log: function(){
			// 真正代码略
			console.log( Array.prototype.join.call( arguments ) );
		}
	};

	var miniConsole = (function(){
		var cache = [];
		var handler = function( ev ){
			if ( ev.keyCode === 113 ){
				var script = document.createElement( 'script' );
				script.onload = function(){
					for ( var i = 0, fn; fn = cache[ i++ ]; ){
						fn();
					}
				};
				script.src = 'miniConsole.js';
				document.getElementsByTagName( 'head' )[0].appendChild( script );
				document.body.removeEventListener( 'keydown', handler );// 只加载一次miniConsole.js
			}
		};
		document.body.addEventListener( 'keydown', handler, false );
		return {
			log: function(){
				var args = arguments;
				cache.push( function(){
					return miniConsole.log.apply( miniConsole, args );
				});
			}
		}
	})();


	miniConsole.log( 11 ); // 开始打印log
	// miniConsole.js 代码
	miniConsole = {
		log: function(){
		// 真正代码略
		console.log( Array.prototype.join.call( arguments ) );
	}
}



</script>


<script type="text/javascript">
	
	var mult = function(){
		console.log( '开始计算乘积' );
		var a = 1;
		for ( var i = 0, l = arguments.length; i < l; i++ ){
			a = a * arguments[i];
		}
		return a;
	};
	mult( 2, 3 ); // 输出：6
	mult( 2, 3, 4 ); // 输出：24
	//现在加入缓存代理函数：

	var proxyMult = (function(){
		var cache = {};
		return function(){
			var args = Array.prototype.join.call( arguments, ',' );
			if ( args in cache ){
				return cache[ args ];
			}
			return cache[ args ] = mult.apply( this, arguments );
		}
	})();

	proxyMult( 1, 2, 3, 4 ); // 输出：24
	proxyMult( 1, 2, 3, 4 ); // 输出：24


	/**************** 计算乘积 *****************/
	var mult = function(){
		var a = 1;
		for ( var i = 0, l = arguments.length; i < l; i++ ){
			a = a * arguments[i];
		}
		return a;
	};
	/**************** 计算加和 *****************/
	var plus = function(){
		var a = 0;
		for ( var i = 0, l = arguments.length; i < l; i++ ){
			a = a + arguments[i];
		}
		return a;
	};
	/**************** 创建缓存代理的工厂 *****************/
	var createProxyFactory = function( fn ){
		var cache = {};
		return function(){
			var args = Array.prototype.join.call( arguments, ',' );
			if ( args in cache ){
				return cache[ args ];
			}
			return cache[ args ] = fn.apply( this, arguments );
		}
	};

	var proxyMult = createProxyFactory( mult ),
	proxyPlus = createProxyFactory( plus );
	alert ( proxyMult( 1, 2, 3, 4 ) ); // 输出：24
	alert ( proxyMult( 1, 2, 3, 4 ) ); // 输出：24
	alert ( proxyPlus( 1, 2, 3, 4 ) ); // 输出：10
	alert ( proxyPlus( 1, 2, 3, 4 ) ); // 输出：10
</script>